/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.casspringbootdemo.demos.web;

import com.example.casspringbootdemo.demos.entitiy.CasUser;
import com.example.casspringbootdemo.demos.utils.CasUserUtils;
import org.jasig.cas.client.validation.Assertion;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author hejun
 */
@Controller
public class BasicController {

    @Value("${cas.server-url-prefix}")
    private String casServerUrlPrefix;
    @Value("${cas.server-login-url}")
    private String casServerLoginUrl;
    @Value("${cas.client-host-url}")
    private String clientHostUrl;
    /**
     * 获取用户信息
     *
     * @param request
     * @return
     */
    @GetMapping("/api/user-info")
    public String getUserInfo(HttpServletRequest request) {
        String username = CasUserUtils.getUsername(request);
        String email = (String) CasUserUtils.getAttribute(request, "email");
        CasUser casUser = CasUserUtils.getCasUser(request);
        return "Hello, " + username + "! Your email is: " + email + "! Your casUser is: " + casUser;
    }

    /* *
     * 认证接口
     */
    @GetMapping("/auth")
    public String auth(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        Assertion assertion = (Assertion) session.getAttribute("_const_cas_assertion_");
        response.setHeader("Content-type", "application/json;charset=UTF-8");
        response.setCharacterEncoding("utf-8");
        response.setStatus(200);
        if (assertion != null) {
            System.out.println("用户已登录");
            return "ok";
        } else {
            try {
                response.setStatus(401);
                response.getWriter().print(casServerLoginUrl+"?service="+clientHostUrl);
                return casServerLoginUrl+"?service="+clientHostUrl;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "no";
    }

    /**
     * 登出接口
     *
     * @param request
     * @param response
     * @param session
     * @return
     */
    @GetMapping("/logout")
    public RedirectView logout(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        session.invalidate();
        return new RedirectView( casServerUrlPrefix + "/logout?service=" + clientHostUrl+"/auth?redirectUrl="+clientHostUrl);
    }

}
